"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : 升主过程中，dn出现故障
Create At   : 2023/04/11
Owner       : opentestcase043
Description :
        1.查询集群状态
        2.关闭主数据库（主机执行）
        3.进行switchover操作
        4.查询集群状态
        5.查询原始主机上的数据库进程
        6.升主过程中（还原初始状态），dn有故障
        7.清理环境
Expect      :
        1.集群状态正常
        2.停止数据库成功
        3.操作成功
        4.原主节点数据库状态显示Manually stopped，资源状态显示Deleted
        5.进程不存在
        6.升主失败
        7.清理环境成功
History     :
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.con = Constant()

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:关闭主数据库（主机执行）; expect:停止数据库成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop', '-n 1',
                                        f'-D {macro.DB_INSTANCE_PATH}')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_instance_success_msg,
                      status, '执行失败:' + text)

        text = '-----step3:进行switchover操作; expect:对应的节点状态offline-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)

        text = '-----step4:查询集群状态; '\
               'expect:原主节点数据库状态显示Manually stopped，'\
               '资源状态显示Deleted-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('dms_res  6001      Deleted', status, '执行失败:' + text)
        self.assertIn('dss      20001     Deleted', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.assertIn('P Down    Manually stopped', res, '执行失败:' + text)

        text = '-----step5:查询原始主机上的数据库进程; expect:进程不存在-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        res = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.assertIn('P Down    Manually stopped', res, '执行失败:' + text)

        text = '-----step6:升主过程中（还原初始状态），dn有故障; '\
               'expect:升主失败-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn('instances have not been switched completely',
                      status, '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        text = '-----step6:清理环境; expect:清理环境成功-----'
        LOG.info(text)
        status1 = Primary_SH.exec_cm_ctl('start', '-n 1',
                                         f'-D {macro.DB_INSTANCE_PATH}')
        time.sleep(20)
        status2 = Primary_SH.exec_cm_ctl('switchover', '-a')
        status3 = Primary_SH.exec_cm_ctl('query', '-Cv')
        res = status3.splitlines()[-1].split('|')[0].split('6001')[-1]
        LOG.info(status1)
        LOG.info(status2)
        LOG.info(status3)
        self.assertIn(self.con.cm_start_node_success_msg, status1,
                      '执行失败:' + text)
        self.assertIn(self.con.cm_switchover_success_msg, status2,
                      '执行失败:' + text)
        self.assertIn('cluster_state   : Normal', status3, '执行失败:' + text)
        self.assertIn('P Primary Normal', res, '执行失败:' + text)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
